package com.jnsw.testkotlin;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import com.jnsw.testkotlin.adapter.SimpleTreeAdapter;
import com.jnsw.testkotlin.bean.Node;
import com.jnsw.testkotlin.bean.TreeBean;
import com.jnsw.testkotlin.bean.TreeListViewAdapter;

import java.util.ArrayList;
import java.util.List;

public class TreeActivity extends AppCompatActivity {

    ListView tree;
    SimpleTreeAdapter mAdapter;

    private List<TreeBean> mDatas = new ArrayList<TreeBean>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tree);
        tree= (ListView) findViewById(R.id.tree);
        initDatas();
        try {
            mAdapter = new SimpleTreeAdapter<TreeBean>(tree,this.getApplication(),mDatas,1);
            mAdapter.setOnTreeNodeClickListener(new TreeListViewAdapter.OnTreeNodeClickListener() {
                @Override
                public void onClick(Node node, int position) {
                    if (node.isExpand() || node.isLeaf()){//展开或者是叶子节点时弹出
                        Toast.makeText(getApplicationContext(),node.getName(),Toast.LENGTH_SHORT).show();
                    }
                }
            });

            tree.setAdapter(mAdapter);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    private void initDatas() {
        mDatas = new ArrayList<>();
        mDatas.add(new TreeBean(1,0,"一"));
        mDatas.add(new TreeBean(2,0,"二"));
        mDatas.add(new TreeBean(3,0,"三"));
        mDatas.add(new TreeBean(11,1,"一1"));
        mDatas.add(new TreeBean(12,1,"一2"));
        mDatas.add(new TreeBean(13,1,"一3"));
        mDatas.add(new TreeBean(21,2,"二1"));
        mDatas.add(new TreeBean(211,21,"二1一"));
        mDatas.add(new TreeBean(22,2,"二2"));
        mDatas.add(new TreeBean(23,2,"二3"));
        mDatas.add(new TreeBean(33,3,"三3"));
        mDatas.add(new TreeBean(333,33,"三3三"));
    }

}
